package com.yyw.foodie.controller.center;

import com.yyw.foodie.common.Result;
import com.yyw.foodie.service.OrderStatusService;
import com.yyw.foodie.service.OrdersService;
import com.yyw.foodie.utils.PagedGridResult;
import com.yyw.foodie.vo.center.order.OrderStatusNumsVo;
import com.yyw.foodie.vo.center.order.OrderTrend;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 易煜玮
 * @ClassName UserCenterController.java
 * @Description 用户中心订单相关接口
 * @createTime 2021年06月03日 11:24:00
 */
@Api(value = "用户中心相关订单接口", tags = "用户中心订单相关的接口")
@RestController
@RequestMapping("/myorders")
public class OrderCenterController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderStatusService orderStatusService;

    @ApiOperation(value = "分页获取用户的订单信息", notes = "可以根据订单状态获取订单的信息")
    @PostMapping("/query")
    public Result getOrderByPage(@ApiParam(value = "用户ID", required = true) @RequestParam("userId") String userId,
                                 @ApiParam(value = "订单状态") @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                 @ApiParam(value = "当前页", defaultValue = "1")
                                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @ApiParam(value = "显示条数", defaultValue = "10")
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数，缺少用户ID");
        }
        PagedGridResult orderInfo = ordersService.getOrderInfo(userId, orderStatus, page, pageSize);
        return Result.ok(orderInfo);
    }


    @GetMapping("/deliver")
    @ApiOperation(value = "商家发货", notes = "自己手动调用模拟商家发货")
    public Result merchantDeliver(@ApiParam(value = "订单ID") @RequestParam("orderId") String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return Result.errorMsg("非法参数 orderId");
        }
        if (ordersService.deliver(orderId)) {
            return Result.ok();
        }
        return Result.errorMsg("订单发货失败,请稍后再试~");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除订单", notes = "删除订单")
    public Result deleteOrder(@ApiParam(value = "订单ID") @RequestParam("orderId") String orderId,
                              @ApiParam(value = "用户ID") @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数");
        }
        if (ordersService.deleteOrder(userId, orderId)) {
            return Result.ok();
        }
        return Result.errorMsg("删除订单失败,请稍后再试~");
    }


    @PostMapping("/confirmReceive")
    @ApiOperation(value = "用户确认收货", notes = "用户确认收货")
    public Result confirmReceive(@ApiParam(value = "订单ID") @RequestParam("orderId") String orderId,
                                 @ApiParam(value = "用户ID") @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数");
        }
        if (ordersService.confirmReceive(userId, orderId)) {
            return Result.ok();
        }
        return Result.errorMsg("确认收货失败,请稍后再试~");
    }


    @PostMapping("/statusCounts")
    @ApiOperation(value = "获取订单状态数概览", notes = "根据用户获取订单状态数总览")
    public Result getOrderStatusNums(@ApiParam(value = "用户ID") @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数");
        }
        OrderStatusNumsVo numsVo = orderStatusService.getOrderStatusNums(userId);
        return Result.ok(numsVo);
    }


    @PostMapping("/trend")
    @ApiOperation(value = "查看订单动向", notes = "查看订单动向")
    public Result getOrderTrend(@ApiParam(value = "用户ID") @RequestParam("userId") String userId,
                                @ApiParam(value = "当前页", defaultValue = "1")
                                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @ApiParam(value = "显示条数", defaultValue = "10")
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return Result.errorMsg("非法参数");
        }
        PagedGridResult result = ordersService.getOrderTrendPage(userId, page, pageSize);
        return Result.ok(result);
    }
}
